package com.jiashihui.schema_lgorithm.schema.prototype;

import java.io.*;

/**
 * @author jiashihui
 * @原型模式: 就是将一个对象作为原型，对其进行复制、克隆，产生一个和原对象类似的新对象
 * @date 2019/03/14
 */
public class DJIUAVPrototype implements Cloneable, Serializable {

    private static final long serialVersionUID = 4296023659365503203L;
    private String name;
    private SerializableObject object;

    public DJIUAVPrototype() {
        super();
    }

    /**
     * 浅复制
     *
     * @author Kevin
     */
    public Object clone() throws CloneNotSupportedException {
        DJIUAVPrototype dJIUAV = (DJIUAVPrototype) super.clone();
        return dJIUAV;
    }

    /**
     * 深复制
     *
     * @author Kevin
     */
    public Object deepClone() throws IOException, ClassNotFoundException {
		/* 写入当前对象的二进制流 */
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(this);
		/* 读取二进制流产生的新对象 */
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        return ois.readObject();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public SerializableObject getObject() {
        return object;
    }

    public void setObject(SerializableObject object) {
        this.object = object;
    }

    @Override
    public String toString() {
        return "型号：" + name + ":" + object.getName();
    }
}
